package hhm.android.machine

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import siau.android.base.*
import siau.android.library.KeyString

class MatchSuccessActivity : SBBaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        setContentView(R.layout.activity_match_success)

        findViewById<FrameLayout>(siau.android.base.R.id.top_back).setOnClickListener {
            onBackPressed()
        }
        findViewById<TextView>(siau.android.base.R.id.top_title).text = "连接成功"
        findViewById<View>(siau.android.base.R.id.title_bar_line).visibility = View.VISIBLE

        when (intent.getIntExtra(KeyString.type, 1)) {
            1 -> {
                val iv: ImageView = findViewById(R.id.activity_match_success_iv_weight)
                iv.setImageResource(R.mipmap.match_success_weight)
            }
            2 -> {
                val iv: ImageView = findViewById(R.id.activity_match_success_iv_height)
                iv.setImageResource(R.mipmap.match_success_height)
            }
            3 -> {
                val iv: ImageView = findViewById(R.id.activity_match_success_iv_bluetooth)
                iv.setImageResource(R.mipmap.match_success_toothbrush)
            }
        }
    }

    override fun onResume() {
        super.onResume()

        handler.sendEmptyMessageDelayed(0, 3000)
    }

    private val handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            onBackPressed()
        }
    }

    override fun onBackPressed() {
        handler.removeCallbacksAndMessages(null)

        SBApplication.hasWeightMachine = false
        SBApplication.hasHeightMachine = false
        SBApplication.hasToothBrushMachine = false
        SBApplication.userData.deviceList?.forEach { d ->
            when (d.deviceType) {
                1 -> {
                    SBApplication.hasWeightMachine = true
                }
                2 -> {
                    SBApplication.hasHeightMachine = true
                }
                3 -> {
                    SBApplication.hasToothBrushMachine = true
                }
            }
        }

        SBApplication.refreshMainActivityUI = true
        setResult(1024)
        super.onBackPressed()
    }
}